<?php

namespace common\models;

use common\helpers\commonApi;
use common\helpers\TransformApi;
use common\helpers\ValidateApi;
use Yii;

/**
 * This is the model class for table "{{%address}}".
 *
 * @property int $id
 * @property int|null $userid 用户id
 * @property string|null $province 省
 * @property string|null $city 市
 * @property string|null $county 县
 * @property string|null $address 详细地址
 * @property string|null $long 经度
 * @property string|null $lat 纬度
 * @property int|null $status 状态
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 修改时间
 * @property string|null $consignee 联系人
 * @property string|null $mobile 联系电话
 * @property int|null $campus_id 学校id
 * @property string|null $campus_name 学校名称
 * @property int|null $sex 性别
 */
class Address extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%address}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['userid', 'status', 'campus_id','sex'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['province', 'city', 'county', 'address', 'long', 'lat', 'consignee', 'mobile', 'campus_name'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'userid' => '用户id',
            'province' => '省',
            'city' => '市',
            'county' => '县',
            'address' => '详细地址',
            'long' => '经度',
            'lat' => '纬度',
            'status' => '状态',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
            'consignee' => '联系人',
            'mobile' => '联系电话',
            'campus_id' => '学校id',
            'campus_name' => '学校名称',
            'sex' => '性别',
        ];
    }

    /**
     * 添加
     * @param $userid 用户id
     * @return array
     */
    public function Add($userid,$params)
    {
        if (empty($params['campus_id'])){
            return commonApi::message('请选择校区',false);
        }
        if (empty($params['address'])){
            return commonApi::message('请输入楼号/门牌号',false);
        }
        if (empty($params['consignee'])){
            return commonApi::message('请输入联系人',false);
        }
        if (empty($params['mobile'])){
            return commonApi::message('请输入手机号',false);
        }
        if (!ValidateApi::isMobilePhone($params['mobile'])){
            return commonApi::message('手机号输入错误',false);
        }
        if (empty($params['sex'])){
            return commonApi::message('请选择性别',false);
        }
        $model = new self();
        $res = $this->location($params['campus_id'],$params['address']);
        if (!$res['success']){
            return $res;
        }
        $model->load($res['data'],'');
        $model->consignee = $params['consignee'];
        $model->mobile = $params['mobile'];
        $model->status = 1;
        $model->sex = $params['sex'];
        $model->address = $params['address'];
        $model->userid = $userid;
        if ($model->save()){
            return commonApi::message('添加成功');
        }
        return commonApi::message(commonApi::getModelError($model),false);
    }

    /**
     * 定位
     * @param $campus_id 校区id
     * @param $address 详细地址
     * @return array
     */
    public function location($campus_id,$address)
    {
        $campus = Campus::find()->where(['id'=>$campus_id,'status'=>1])->one();
        if (!$campus){
            return commonApi::message('校区不存在',false);
        }
        $key = System::getModel()->service_map_key;
        if (!$key){
            return commonApi::message('请配置服务端地图key',false);
        }
        $addr = $campus->province.$campus->city.$campus->county.$campus->address.$address;
        $result = TransformApi::location($addr,$key);
        if ($result['success']){
            $result['data']['province'] = $campus->province;
            $result['data']['city'] = $campus->city;
            $result['data']['county'] = $campus->county;
            $result['data']['address'] = $address;
            $result['data']['campus_name'] = $campus->name;
            $result['data']['campus_id'] = $campus->id;
        }
        return $result;
    }

    /**
     * 修改
     * @param $userid
     * @param $address_id
     * @param $campus_id
     * @param $address
     * @param $consignee
     * @param $mobile
     * @param $sex
     * @return array
     */
    public function edit($userid,$address_id,$params)
    {
        if (empty($params['campus_id'])){
            return commonApi::message('请选择校区',false);
        }
        if (empty($params['address'])){
            return commonApi::message('请输入楼号/门牌号',false);
        }
        if (empty($params['consignee'])){
            return commonApi::message('请输入联系人',false);
        }
        if (empty($params['mobile'])){
            return commonApi::message('请输入手机号',false);
        }
        if (!ValidateApi::isMobilePhone($params['mobile'])){
            return commonApi::message('手机号输入错误',false);
        }
        if (empty($params['sex'])){
            return commonApi::message('请选择性别',false);
        }
        $model = self::findOne(['id'=>$address_id,'userid'=>$userid]);
        if (!$model){
            return commonApi::message('地址不存在',false);
        }
        if ($model->campus_id!==$params['campus_id']){
            $res = $this->location($params['campus_id'],$params['address']);
            if (!$res['success']){
                return $res;
            }
        }
        $model->load($res['data'],'');
        $model->consignee = $params['consignee'];
        $model->mobile = $params['mobile'];
        $model->sex = $params['sex'];
        $model->address = $params['address'];
        if ($model->save()){
            return commonApi::message('修改成功');
        }
        return commonApi::message(commonApi::getModelError($model),false);
    }
}
